package com.jiangmiao.babaisyou.level;

import com.jiangmiao.babaisyou.domain.Closure;
import com.jiangmiao.babaisyou.domain.People;
import com.jiangmiao.babaisyou.domain.Text;
import com.jiangmiao.babaisyou.ui.GameFrame;
import sun.awt.image.ToolkitImage;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 绘制外围墙体
 */
public class GameClosure {
    private List<Closure> closureList;

    /**
     * 构造方法，初始化墙体集合
     */
    public GameClosure() {
        closureList = new ArrayList<>();
        for (int i = 0; i < 19; i++) {
            closureList.add(new Closure(i * 24, 1 * 24));
            closureList.add(new Closure(i * 24, 18 * 24));
        }
        for (int i = 1; i < 18; i++) {
            closureList.add(new Closure(0, i * 24));
            closureList.add(new Closure(18 * 24, i * 24));
        }
    }

    /**
     * 绘制墙体
     *
     * @param graphics
     */
    public void draw(Graphics graphics, GamePeople gamePeople, GameText gameText) {

        List<People> peopleList = gamePeople.getPeopleList();
        List<Text> textList = gameText.getTextList();
        /**
         * people碰撞判断
         */
        for (int i = 0; i < peopleList.size(); i++) {
            People people = peopleList.get(i);
            for (int j = 0; j < closureList.size(); j++) {
                Closure closure = closureList.get(j);
                // 碰撞判断
                if (people.getX() == closure.getX() && people.getY() == closure.getY()) {
                    /**
                     * 反弹逻辑
                     */
                    if (people.up) {
                        people.setY(people.getY() + 24);//y += 24;
                    } else if (people.down) {
                        people.setY(people.getY() - 24);//y -= 24;
                    } else if (people.right) {
                        people.setX(people.getX() - 24);//x -= 24;
                    } else if (people.left) {
                        people.setX(people.getX() + 24);//x += 24;
                    }
                }
            }
        }

        /**
         * text碰撞
         */
        for (int i = 0; i < textList.size(); i++) {
            Text text = textList.get(i);
            for (int j = 0; j < closureList.size(); j++) {
                Closure closure = closureList.get(j);
                if (text.getX() == closure.getX() && text.getY() == closure.getY()) {
                    if (text.up) {
                        text.setY(text.getY() + 24);//y += 24;
                    } else if (text.down) {
                        text.setY(text.getY() - 24);//y -= 24;
                    } else if (text.right) {
                        text.setX(text.getX() - 24);//x -= 24;
                    } else if (text.left) {
                        text.setX(text.getX() + 24);//x += 24;
                    }
                }
            }
        }


        URL resource = GameFrame.class.getResource("asset/icon/LAVA.gif");
        Image image = new ImageIcon(resource).getImage();
        for (int i = 0; i < closureList.size(); i++) {
            Closure closure = closureList.get(i);
            graphics.drawImage(image, closure.getX(), closure.getY(), null);
        }
    }

    public static void main(String[] args) {
        URL resource = GameFrame.class.getResource("asset/logo/logo.png");
        System.out.println(resource);
    }

}
